/* (c)2010-2012 Andrea Caltroni (andrea.caltroni@pd.infn.it), INFN (www.infn.it)
 *
 * This file is part of the IGI Survey Portlet (from here on shortened to IGISP).
 * IGISP is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) any 
 * later version.
 *
 * IGISP is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IGISP. If not, see <http://www.gnu.org/licenses/>.
 */
package it.infn.grid.liferay.survey.modules.admin.controller;

import java.util.List;

import javax.portlet.RenderRequest;

import org.ajm.spring.survey.modules.admin.model.Answer;
import org.ajm.spring.survey.modules.admin.service.AnswerService;
import org.ajm.spring.survey.modules.takesurvey.service.UserChoiceService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.bind.PortletRequestDataBinder;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

/**
 * The controller used to show the paged list of custom descriptions for an answer
 * of mode {@link org.ajm.spring.survey.modules.admin.model.AnswerMode#CUSTOM_DESC AnswerMode.CUSTOM_DESC}.
 * 
 * @author Andrea Caltroni, andrea.caltroni@pd.infn.it, INFN
 */
@RequestMapping("VIEW")
@Controller(value="customDescriptionController")
public class CustomDescriptionController {

    // ------------------------------------------------------------------------|
    // --- CONSTANTS ----------------------------------------------------------|
    // ------------------------------------------------------------------------|
    
    //public static final int DEFAULT_PAGE_SIZE = 15;
    
    // --- CLASS VARIABLES ----------------------------------------------------|
    // ------------------------------------------------------------------------|
    // ------------------------------------------------------------------------|

    static Logger logger = Logger.getLogger(CustomDescriptionController.class);
    
    // ------------------------------------------------------------------------|
    // --- INSTANCE VARIABLES -------------------------------------------------|
    // ------------------------------------------------------------------------|
    
    private int pageSize = SavePreferencesController.NUM_CUST_DESCS_PER_PAGE;
    
    @Autowired
    @Qualifier("answerService")
    private AnswerService answerService;
    
    @Autowired
    @Qualifier("userChoiceService")
    private UserChoiceService userChoiceService;
    
    // ------------------------------------------------------------------------|
    // --- CONSTRUCTORS -------------------------------------------------------|
    // ------------------------------------------------------------------------|

    // ------------------------------------------------------------------------|
    // --- CLASS METHODS ------------------------------------------------------|
    // ------------------------------------------------------------------------|

    // ------------------------------------------------------------------------|
    // --- INSTANCE METHODS ---------------------------------------------------|
    // ------------------------------------------------------------------------|

    /**
     * Setup binder for adding/editing answers.
     */
    @InitBinder
    protected void initBinder(PortletRequestDataBinder binder) {
        
        //binder.setAllowedFields(new String[] { "description", "question", "position", "type" });
    }

    // ------------------------------------------------------------------------|
    
    /**
     * Gets the command object to be added to the model.
     * 
     * @param answerId
     * @return The list of IDs for a page of UserAnswers.
     */
    @ModelAttribute("pagedListHolder")
    public PagedListHolder<String> getCommandObject(@RequestParam("answerID") Long answerId) {
        
        Answer answer = getAnswerService().findById(answerId);
        
        List<String> userAnswers = getUserChoiceService().getAllCustomDescriptions(answer);
        PagedListHolder<String> pagedListHolder = new PagedListHolder<String>(userAnswers);
        
        return pagedListHolder;
    }

    // ------------------------------------------------------------------------|
    
    /**
     * Shows a page from the list of user descriptions for an answer.
     * 
     * @param answerId 
     * @param pagedListHolder 
     * @param pageNumber 
     * @param model 
     * @param request 
     * @return The name of the view.
     */
    @RenderMapping(params="action=viewCustomDescriptions")
    public String viewCustomDescriptions(@RequestParam("answerID") Long answerId, 
            @RequestParam("p") int pageNumber, 
            @ModelAttribute("pagedListHolder") PagedListHolder<String> pagedListHolder,
            Model model, RenderRequest request) {
        
        // retrieve from the user preferences
        setPageSize(new Integer(request.getPreferences().getValue("noOfUserDescsPerPage", "" + SavePreferencesController.NUM_CUST_DESCS_PER_PAGE)).intValue());
        
        Answer answer = getAnswerService().findById(answerId);
        model.addAttribute("answer", answer);
        model.addAttribute("pageSize", getPageSize());
        model.addAttribute("p", pageNumber);
        
        pagedListHolder.setPageSize(getPageSize());
        pagedListHolder.setPage(pageNumber);
        model.addAttribute("pagedListHolder", pagedListHolder);
        
        return "customDescriptionsView";
    }
    
    // --- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --|
    // ---[  GETTERS AND SETTERS  ]--------------------------------------------|
    // --- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --|

    /**
     * @return the answerService
     */
    public AnswerService getAnswerService() {
        return answerService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @param answerService the answerService to set
     */
    public void setAnswerService(AnswerService answerService) {
        this.answerService = answerService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @return the userChoiceService
     */
    public UserChoiceService getUserChoiceService() {
        return userChoiceService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @param userChoiceService the userChoiceService to set
     */
    public void setUserChoiceService(UserChoiceService userChoiceService) {
        this.userChoiceService = userChoiceService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @return the pageSize
     */
    public int getPageSize() {
        return this.pageSize;
    }

    // ------------------------------------------------------------------------|

    /**
     * @param pageSize the pageSize to set
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    // ------------------------------------------------------------------------|

}
